# -*- coding: utf-8 -*-
from rest_framework.decorators import api_view
from rest_framework.response import Response

from django.core.files.storage import default_storage
from django.core.files.base import ContentFile

import mimetypes

import os
import uuid
import magic

import settings


@api_view(['POST'])
def solve(request):
    img = request.FILES['img']

    ext = mimetypes.guess_extension(img.content_type)

    filename = '%s%s' % (uuid.uuid4().hex, ext)
    save_file = default_storage.save(filename, ContentFile(img.read()))

    file_path = os.path.join(settings.MEDIA_ROOT, save_file)

    res = magic.apply_model(file_path)

    return Response({
        'results': [
            (res, 1),
        ]
    })
